<?php

namespace common\models;

use himiklab\sitemap\behaviors\SitemapBehavior;
use yii\helpers\Url;
use Yii;

/**
 * This is the model class for table "{{%page}}".
 *
 * @property integer $id
 * @property integer $cid
 * @property string $title
 * @property string $author
 * @property string $description
 * @property string $content
 * @property string $slug
 * @property integer $views
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $enabled
 * @property string $sitemap_url
 */
class Page extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%page}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['cid', 'views', 'created_at', 'updated_at', 'enabled'], 'integer'],
            [['title'], 'required'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 100],
            [['author', 'slug'], 'string', 'max' => 20],
            [['description', 'sitemap_url'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => Yii::t('common', 'ID'),
            'cid' => Yii::t('common', '职业ID'),
            'title' => Yii::t('common', '标题'),
            'author' => Yii::t('common', '作者'),
            'description' => Yii::t('common', '摘要'),
            'content' => Yii::t('common', '内容'),
            'slug' => Yii::t('common', '路由(Slug)'), 
            'views' => Yii::t('common', '浏览量'),
            'created_at' => Yii::t('common', '创建时间'),
            'updated_at' => Yii::t('common', '更新时间'),
            'enabled' => Yii::t('common', '允许访问'),
            'sitemap_url' => Yii::t('common', 'sitemap url'), 
        ];
    }

    // 用于生成sitemap url字段内容
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            $data = Yii::$app->request->post('Page');
            if (!empty($data)) {
                //  新纪录插入sitemap_url内容
                if ($this->isNewRecord) {
                    $connection = \Yii::$app->db;
                    //  获取表中最后一条记录ID
                    $result = $connection->createCommand("Select `id` from `page` order by `id` desc limit 1")
                            ->queryOne();
                    $id = strval($result['id']) + 1;
                    $this->sitemap_url = Yii::$app->params['frontend_domain'] . '/page/' . $id;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置自动更新时间   created_at | updated_at 字段类型为 int(11) 
     */
    public function behaviors() {
        return [
            \yii\behaviors\TimestampBehavior::className(),
            'sitemap' => [
                'class' => SitemapBehavior::className(),
                'scope' => function ($model) {
                    /** @var \yii\db\ActiveQuery $model */
                    $model->select(['sitemap_url', 'updated_at']);
                },
                        'dataClosure' => function ($model) {
                    /** @var self $model */
                    return [
                        'loc' => Url::to($model->sitemap_url, true),
                        'lastmod' => strtotime(date('Y/m/d h:i:s', $model->updated_at)),
                        'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
                        'priority' => 0.6
                    ];
                }
                    ],
                ];
            }

        }
        